package com.bysc.userservice.controller;

import com.alibaba.fastjson.JSON;
import com.bysc.baseservice.handler.BaiYunException;
import com.bysc.commonutils.ResultCode;
import com.bysc.commonutils.Results;
import com.bysc.commonutils.constant.MessageConstant;
import com.bysc.commonutils.message.NotificationMessage;
import com.bysc.userservice.client.JoinClient;
import com.bysc.userservice.websocket.WebSocketServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

@RestController
@Api(tags = "发送通知管理")
@RequestMapping("/websocketservice/websocket-notice")
public class WebsocketNoticeController {
    @Autowired
    private WebSocketServer webSocketServer;
    @Autowired
    private JoinClient joinClient;

    @ApiOperation(value = "对一个用户发送信息")
    @PostMapping("sendToOne")
    public Results sendToOne(@ApiParam(name = "message", value = "通知信息", required = true) @RequestBody NotificationMessage message) {
        try {
            webSocketServer.sendToOne(message);
            return Results.ok().message(MessageConstant.SEND_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.SEND_FAIL);
        }
    }

    @ApiOperation(value = "对全体用户发送信息")
    @PostMapping("sendToAll")
    public Results sendToAll(@ApiParam(name = "message", value = "通知信息", required = true) @RequestBody NotificationMessage message) {
        try {
            webSocketServer.sendToAll(message);
            return Results.ok().message(MessageConstant.SEND_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.SEND_FAIL);
        }
    }

    @ApiOperation(value = "对协会成员发送信息")
    @PostMapping("sendToClub")
    public Results sendToClub(@ApiParam(name = "message", value = "通知信息", required = true) @RequestBody NotificationMessage message) {
        try {
            ArrayList<String> ids = (ArrayList<String>) joinClient.getAllJoin().getData().get("ids");
            webSocketServer.sendToClub(message, ids);
            return Results.ok().message(MessageConstant.SEND_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.SEND_FAIL);
        }
    }

    @ApiOperation(value = "获取在线人数")
    @PostMapping("getOnLine")
    public Results getOnLine() {
        try {
            Integer count = webSocketServer.getOnLine();
            return Results.ok().data("count", count).message(MessageConstant.QUERY_USERINFO_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.QUERY_USERINFO_FAIL);
        }
    }
    /**
     * 每3秒执行一次
     */
    @Scheduled(cron = "0/3 * * * * ? ")
    public void sendTime() {
        System.out.println("sendTime执行了.... " + new Date());
        Date date = new Date(System.currentTimeMillis());
        webSocketServer.sendTime(date);
    }

}
